$(document).ready(function() {

      clicks();
      loadData2();
      var song_name;
      var song_url;
      var song_lenght;
      var artists_name;
      
      var mp3audio;
      var i =0;
      var j =0;

           
        $(".icon-qianjin").click(function(){
             i= i+1;
             if(i > song_lenght.length )
             {
                 i=0;
             }
             loadData2();
             song_start();
            $("#audio").attr("autoplay","autoplay");
        });

        $(".icon-houtui").click(function(){
              i= i-1;
              if(i <song_lenght.length)
              {
                i=0;
              }
              loadData2();
             song_start();
            $("#audio").attr("autoplay","autoplay");
        });

                
            function loadData2(){
                $.ajax({
                    type:"GET",
                    url:"http://192.168.3.5:8765/web?id=584737619",
                    dataType: "json",
                    success: function(data) {
                    
                            var html_tupian =template('backgroud_all',data);
                            $('.all').html(html_tupian);

                        
                            var html_img =template('control_img',data);
                            $('.img-control').html(html_img)

                            var html_list =template('list_song',data);
                            $('.song-list').append(html_list);

                            var html_message =template('message_list',data)
                            $('.message-list').html(html_message);
                          
                            song_lenght = data["result"]["tracks"]
                          
                            song_name  =  song_lenght[i]["transNames"][j];
                            $("#message-song-span").html("歌曲："+song_name);

                            song_url =data["result"]["tracks"][i]["mp3Url"];
                            $("#audio").attr('src',song_url);

                            $("#nav-transName").html(song_name);

                            artists_name =data["result"]["tracks"][i]["artists"][0]["name"]
                            $("#nav-artists-name").html(artists_name);


                            $(".neibu").click(function(){
                                 var mp3url = $(this).attr('value'); 
                                 var li_value = $(this).find('.song_name').attr("data-index");
                                 var name_song_2 =$(this).find('.song-name').html()
                                 var name_art =$(this).find('.people-name').html()
                                 mp3audio = $("#audio").attr('src')
                                 $("#message-song-span").html("歌曲："+name_song_2);
                                 $("#nav-transName").html(name_song_2);
                                 $("#nav-artists-name").html(name_art);
                                 $("#audio").attr('src',mp3url);  
                                 song_start();

                                                                          
                                if(li_value = i)   
                                   {
                                       $("this").addClass('bofang-style');
                                   } 
                                        
                              });
   

                    },
                    error: function (error) {
                        easyTip('网络错误');
                        loading = false;
                        hasNext = true;
                    }
                });
            }

 
     $(".icon-sanheng").click(function(){
           $(".song-list").css("display","block");
     });

    $(".list-close").click(function(){
           $(".song-list").css("display","none");
     });

    $(".touming").click(function(){
           $(".song-list").css("display","none");
     });
     $(".icon-sandian").click(function(){
           $(".song-message").css("display","block");
     });

    $(".message-touming").click(function(){
           $(".song-message").css("display","none");
     });
     
	function clicks() {
                      
      $("#control").click(function() {
                var audio = document.getElementById("audio");
                if ($("#audio").hasClass("play")) {
                            $("#audio").addClass("pause").removeClass("play");
                            $("#control").addClass("icon-bofang").removeClass("icon-zanting1");
                            $(".face-img").addClass("face-img-circle").removeClass("face-img-pause");
                            $(".changpian").addClass("face-img-circle").removeClass("face-img-pause");
                            $("#need").addClass("needle-start")
                            song_start();   
                        } else {
                            $("#audio").addClass("play").removeClass("pause");
                            $("#control").addClass("icon-zanting1").removeClass("icon-bofang");
                            $("#need").addClass("needle-end");
                            song_stop();
                        }
                    })
                 }


            function song_start(){

                            var vid = document.getElementById("audio")
                            $(".song-bar-bottom-runing").addClass("song-bar song-bar-running").removeClass("song-bar-pause");
                            var song_duration =vid.duration;
                            audio.play();
                           
                            vid.ontimeupdate = function() {myFunction()}; 

                            function myFunction() {  
                             var one_s = (237/song_duration);
                             var song_currenttime =vid.currentTime;
                             var width_bar = one_s*song_currenttime;
                             var time_second =parseInt(song_currenttime % 60);
                             var time_min =parseInt(song_currenttime / 60);
                             var time_second_surplus = parseInt((song_duration - song_currenttime)%60);
                             var time_min_surplud = parseInt((song_duration - song_currenttime)/60);
                             if (time_second<10)
                             {
                                time_second =("0"+time_second);
                             }
                             else if(time_second_surplus<10)
                             {
                                time_second_surplus =("0"+time_second_surplus);
                             }
                        
                              document.getElementById("song-start").innerHTML = (":"+time_second);
                              document.getElementById("song-start-m").innerHTML =("0" + time_min);
                              document.getElementById("song-finish").innerHTML =("0" +  time_min_surplud);
                              document.getElementById("song-finish-m").innerHTML =(":" + time_second_surplus);
                              document.getElementById("song_bar_d").style.width= width_bar + 'px';
                           } 

                 }

                 function song_stop(){
                            audio.pause();
                            
                            $(".song-bar-bottom-runing").addClass("song-bar-pause")
                            $(".changpian").addClass("face-img-pause");
                            $(".face-img").addClass("face-img-pause");
                           
                 }

	
});

 